package com.fr.report.core.barcode.line.code128;

import com.fr.report.core.barcode.BarcodeException;
import com.fr.report.core.barcode.BarcodeImpl;
import com.fr.report.core.barcode.BlankModule;
import com.fr.report.core.barcode.CompositeModule;
import com.fr.report.core.barcode.Module;
import java.util.ArrayList;
import java.util.List;

public class Code128Barcode extends BarcodeImpl
{
  public static final int A = 0;
  public static final int B = 1;
  public static final int C = 2;
  public static final int O = 3;
  public static final String SHIFT = "Æ";
  public static final String CHANGE_TO_A = "É";
  public static final String CHANGE_TO_B = "È";
  public static final String CHANGE_TO_C = "Ç";
  public static final String FNC_1 = "Ê";
  protected static final Module START_A = new Module(new int[] { 2, 1, 1, 4, 1, 2 });
  protected static final Module START_B = new Module(new int[] { 2, 1, 1, 2, 1, 4 });
  protected static final Module START_C = new Module(new int[] { 2, 1, 1, 2, 3, 2 });
  protected static final Module STOP = new Module(new int[] { 2, 3, 3, 1, 1, 1, 2 });
  protected static final Module QUIET_SECTION = new BlankModule(10);
  private static final Module[] START = { START_A, START_B, START_C };
  protected static final int[] START_INDICES = { 103, 104, 105 };
  protected static final int[] BUF_SIZES = { 1, 1, 2 };
  protected int startIndex;
  protected int mode;
  private int startingMode;
  private boolean shiftNext;
  private boolean shifted;
  private CharBuffer buf;
  private Module checkDigit;
  private boolean optimising;
  private Accumulator sum;
  private Accumulator index;

  public Code128Barcode(String paramString)
    throws BarcodeException
  {
    this(paramString, 3);
  }

  public Code128Barcode(String paramString, int paramInt)
    throws BarcodeException
  {
    super(paramString);
    if (paramInt == 3)
    {
      this.optimising = true;
      this.mode = 1;
    }
    else
    {
      this.optimising = false;
      this.mode = paramInt;
    }
    this.startingMode = this.mode;
    this.shiftNext = false;
    this.shifted = false;
    this.startIndex = START_INDICES[this.mode];
  }

  public int getCharacterSet()
  {
    return this.startingMode;
  }

  protected String getLabel()
  {
    return this.data;
  }

  protected double getBarcodeWidth(int paramInt)
  {
    return (getQuietZoneWidth() * 2.0D + getSymbolWidth(paramInt));
  }

  protected double getSymbolWidth(int paramInt)
  {
    double d1 = convertToMillimetres(this.barWidth, paramInt);
    double d2 = 11.0D;
    if (this.startingMode == 2)
      d2 = 5.5D;
    return ((d2 * this.data.length() + 35.0D) * d1);
  }

  protected double calculateMinimumBarHeight(int paramInt)
  {
    double d = paramInt * 0.25D;
    return Math.max(0.15D * getSymbolWidth(paramInt), d);
  }

  protected Module[] encodeData()
  {
    this.sum = new Accumulator(this.startIndex);
    ArrayList localArrayList = new ArrayList();
    this.buf = new CharBuffer(BUF_SIZES[this.mode]);
    this.index = new Accumulator(1);
    padDataToEvenLength();
    for (int i = 0; i < this.data.length(); ++i)
    {
      char c = this.data.charAt(i);
      if ((this.optimising) && (this.startingMode == 1))
        if ((i + 1 < this.data.length()) && (isControl(c)) && (this.mode != 0))
          if (this.mode == 1)
            encode(localArrayList, "Æ");
          else
            encode(localArrayList, "É");
        else if ((i + 3 < this.data.length()) && (digitGroupIsNext(i, this.data)) && (this.mode != 2))
          encode(localArrayList, "Ç");
        else if ((i + 1 <= this.data.length()) && (digitGroupEndIsNext(i, this.data)) && (this.mode == 2) && (this.buf.size() != 1))
          encode(localArrayList, "È");
      if (isShiftOrCode(c))
      {
        encode(localArrayList, String.valueOf(c));
        this.buf.clear();
      }
      else
      {
        this.buf.addChar(c);
        if (this.buf.isFull())
        {
          encode(localArrayList, this.buf.toString());
          this.buf.clear();
        }
      }
    }
    this.checkDigit = ModuleFactory.getModuleForIndex(this.sum.getValue() % 103, this.mode);
    this.mode = this.startingMode;
    return ((Module[])localArrayList.toArray(new Module[0]));
  }

  private boolean isShiftOrCode(char paramChar)
  {
    String str = String.valueOf(paramChar);
    return ((str.equals("Æ")) || (str.equals("É")) || (str.equals("È")) || (str.equals("Ç")) || (str.equals("Ê")));
  }

  protected Module calculateChecksum()
  {
    if (this.checkDigit == null)
      encodeData();
    return this.checkDigit;
  }

  protected Module getPreAmble()
  {
    CompositeModule localCompositeModule = new CompositeModule();
    localCompositeModule.add(QUIET_SECTION);
    localCompositeModule.add(START[this.mode]);
    return localCompositeModule;
  }

  protected Module getPostAmble()
  {
    CompositeModule localCompositeModule = new CompositeModule();
    localCompositeModule.add(STOP);
    localCompositeModule.add(QUIET_SECTION);
    return localCompositeModule;
  }

  protected double getQuietZoneWidth()
  {
    return (10.0D * this.barWidth);
  }

  private boolean isControl(char paramChar)
  {
    return Character.isISOControl(paramChar);
  }

  private boolean digitGroupIsNext(int paramInt, String paramString)
  {
    char c1 = paramString.charAt(paramInt);
    char c2 = paramString.charAt(paramInt + 1);
    char c3 = paramString.charAt(paramInt + 2);
    char c4 = paramString.charAt(paramInt + 3);
    return ((Character.isDigit(c1)) && (Character.isDigit(c2)) && (Character.isDigit(c3)) && (Character.isDigit(c4)));
  }

  private boolean digitGroupEndIsNext(int paramInt, String paramString)
  {
    if (paramInt == paramString.length() - 1)
      return true;
    char c1 = paramString.charAt(paramInt);
    char c2 = paramString.charAt(paramInt + 1);
    return (((Character.isDigit(c1)) && (!(Character.isDigit(c2)))) || (!(Character.isDigit(c1))));
  }

  private void padDataToEvenLength()
  {
    if ((this.startingMode == 2) && (this.data.length() % 2 != 0) && (!(containsShiftOrChange(this.data))))
      this.data = '0' + this.data;
  }

  private boolean containsShiftOrChange(String paramString)
  {
    return ((paramString.indexOf("É") >= 0) || (paramString.indexOf("È") >= 0) || (paramString.indexOf("Ç") >= 0) || (paramString.indexOf("Æ") >= 0));
  }

  private void clearShift()
  {
    if (this.shifted)
    {
      this.shifted = false;
      this.shiftNext = false;
      this.mode = shiftMode();
    }
  }

  private void checkShift(Module paramModule)
  {
    if (paramModule instanceof ShiftModule)
    {
      this.mode = shiftMode();
      this.shiftNext = true;
    }
    else if (this.shiftNext)
    {
      this.shifted = true;
    }
  }

  private int shiftMode()
  {
    if (this.mode == 0)
      return 1;
    return 0;
  }

  private void checkCodeChange(Module paramModule)
  {
    if (paramModule instanceof CodeChangeModule)
    {
      this.mode = ((CodeChangeModule)paramModule).getCode();
      this.buf = new CharBuffer(BUF_SIZES[this.mode]);
    }
  }

  private void encode(List paramList, String paramString)
  {
    Module localModule = ModuleFactory.getModule(paramString, this.mode);
    updateCheckSum(paramString);
    checkShift(localModule);
    checkCodeChange(localModule);
    paramList.add(localModule);
    clearShift();
  }

  private void updateCheckSum(String paramString)
  {
    int i = ModuleFactory.getIndex(paramString, this.mode);
    this.sum.add(i * this.index.getValue());
    this.index.increment();
  }

  private double convertToMillimetres(double paramDouble, int paramInt)
  {
    double d = paramInt / 25.399999999999999D;
    return (paramDouble / d);
  }
}